{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing...\n",
      "Got command 16 at 1673630468786375077\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "rospy.Time[1673630468786375077]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mav = None\n",
    "import sys\n",
    "sys.path.append(\"/home/xuhao/d2slam_ws/src/D2SLAM/quadcam_tools/\")\n",
    "import rosbag\n",
    "\n",
    "def get_traj_command_time(bag):\n",
    "    from pymavlink4swarm import MAVLink\n",
    "    import array\n",
    "    global mav\n",
    "    if mav is None:\n",
    "        class fifo(object):\n",
    "            def __init__(self):\n",
    "                self.buf = []\n",
    "            def write(self, data):\n",
    "                self.buf += data\n",
    "                return len(data)\n",
    "            def read(self):\n",
    "                return self.buf.pop(0)\n",
    "        f = fifo()\n",
    "        mav = MAVLink(f)\n",
    "    print(\"Processing...\")\n",
    "    for topic, msg, t in rosbag.Bag(bag).read_messages():\n",
    "        if topic == \"/uwb_node/incoming_broadcast_data\":\n",
    "            m = mav.decode(array.array('B', (msg.data)))\n",
    "            if m.command_type == 16:\n",
    "                print(f\"Got command {m.command_type} at {t}\")\n",
    "                return t\n",
    "    return None\n",
    "bag = \"/media/xuhao/Data/quadcam_7inch_n3_2023_1_14/eight_noyaw_1.bag\"\n",
    "get_traj_command_time(bag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "08ce52785f0fedc81003ce387e097a83d6cc9494681cd746006386992005bb71"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
